package routes

import (
	"gin-students-surrealdb/config"
	"gin-students-surrealdb/controllers"
	"gin-students-surrealdb/repositories"
	"github.com/gin-gonic/gin"
)

func SetupRouter() *gin.Engine {
	r := gin.Default()

	// 初始化数据库
	if err := config.InitDB(); err != nil {
		panic("Failed to connect to database")
	}

	// 初始化仓库和控制器
	studentRepo := repositories.NewStudentRepository(config.DB)
	studentController := controllers.NewStudentController(studentRepo)

	// 路由分组
	api := r.Group("/api")
	{
		v1 := api.Group("/v1")
		{
			students := v1.Group("/students")
			{
				students.POST("", studentController.CreateStudent)
				students.GET("", studentController.GetAllStudents)
				students.GET("/:id", studentController.GetStudent)
				students.PUT("/:id", studentController.UpdateStudent)
				students.DELETE("/:id", studentController.DeleteStudent)
			}
		}
	}

	return r
}
